package com.itheima.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.util.Random;

public class GameFrame extends JFrame implements KeyListener {
    private int arr[][] = new int[4][4];
    private int[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    private int x = 0;  // 记录需要移动的x和y位置
    private int y = 0;

    public GameFrame() {
//        初始化界面
        initJframe();
//        添加menuBar
        initJMenuBar();
//        初始化数据
        initData();
//        添加图片到容器中
        initImage();
    }

    private void initData() {
        Random rand = new Random();
        for (int i = 0; i < data.length; i++) {
            int index = rand.nextInt(data.length);
            int temp = data[index];
            data[index] = data[i];
            data[i] = temp;
        }
        for (int i = 0; i < data.length; i++) {
            if (data[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
            arr[i / 4][i % 4] = data[i];
        }
    }
    private void initImage() {
        //清空原本已经出现的所有图片
        this.getContentPane().removeAll();

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int num = arr[i][j];
                ImageIcon imageIcon = new ImageIcon("puzzleGame\\image\\animal\\animal3\\" + num + ".jpg");
                JLabel jLabel = new JLabel(imageIcon);
                jLabel.setBounds(105 * i + 83, 105 * j + 134, 105, 105);
                this.getContentPane().add(jLabel);
            }
        }
//        先添加的图片会先展示在上边，因此背景图片应该放到最后在添加
        ImageIcon bgcImage = new ImageIcon("puzzleGame\\image\\background.png");
        JLabel jLabelBgc = new JLabel(bgcImage);
        jLabelBgc.setBounds(40, 40, 508, 560);
        this.getContentPane().add(jLabelBgc);

        //刷新一下界面
        this.getContentPane().repaint();
    }

    private void initJMenuBar() {
        JMenuBar jMenuBar = new JMenuBar();

        JMenu functionMenu = new JMenu("功能");
        JMenu aboutMenu = new JMenu("关于我们");

        JMenuItem resetMenuItem = new JMenuItem("重置游戏");
        JMenuItem reloginMenuItem = new JMenuItem("重新登录");
        JMenuItem closeMenuItem = new JMenuItem("关闭游戏");

        JMenuItem accountMenuItem = new JMenuItem("公众号");

        functionMenu.add(resetMenuItem);
        functionMenu.add(reloginMenuItem);
        functionMenu.add(closeMenuItem);

        aboutMenu.add(accountMenuItem);

        jMenuBar.add(functionMenu);
        jMenuBar.add(aboutMenu);

        this.setJMenuBar(jMenuBar);
    }

    private void initJframe() {
        this.setSize(603, 680);
        this.setTitle("拼图游戏");

        //设置界面置顶
//        this.setAlwaysOnTop(true);

//        设置界面居中
        this.setLocationRelativeTo(null);

//        取消默认的居中放置，只有取消了才会按照XY轴的形式添加组件
        this.setLayout(null);

//        设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.addKeyListener(this);

        this.setVisible(true);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
//        37 左，38 上， 39 右，40下
        int code = e.getKeyCode();
        if (code == 40) {
            if (y == 3) {
                return;
            }
            arr[x][y] = arr[x][y + 1];
            arr[x][y + 1] = 0;
            y ++;
            initImage();
        } else if (code == 39) {
            if(x == 3){
                return;
            }
            arr[x][y] = arr[x + 1][y];
            arr[x + 1][y] = 0;
            x++;
            initImage();
        } else if (code == 38) {
            if(y == 0){
                return;
            }
            arr[x][y] = arr[x][y - 1];
            arr[x][y - 1] = 0;
            y--;
            initImage();
        } else if (code == 37) {
            if(x == 0){
                return;
            }
            arr[x][y] = arr[x - 1][y];
            arr[x - 1][y] = 0;
            x--;
            initImage();
        }
    }
}
